<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { axiosrnewmedical } from '@/http/axiosRead';

// 定义部门数据接口
interface Department {
  id: number;
  departmentName: string;
  type: string;
  paretId: number;
  departmentAddr: string;
  registrationFee: number;
  isEmergencyService: boolean;
  isOnline: boolean;
  remark: string;
}

// 定义医院数据接口
interface Hospital {
  id: number;
  hospitalName: string;
  level: string;
  hospitalTypeId: number;
  contactInfo: string;
  region: string;
  image: string;
  detailedAddress: string;
  departmentSetup: Department[];
  numberOfDoctors: number;
  numberOfDepartments: number;
  hospitalIntroduction: string;
  isRecommended: boolean;
}

const router = useRouter();
const hospitals = ref<Hospital[]>([]);
const loading = ref(false);

const goBack = () => {
  router.back();
};

// 获取医院列表
const fetchHospitals = async () => {
  loading.value = true;
  try {
    const response = await axiosrnewmedical.get('/Hospital/SearchHospitals', {
      params: {
        pageIndex: 1,
        pageSize: 20
      }
    });
    
    if (response.data.code === 200 && response.data.pageData) {
      hospitals.value = response.data.pageData;
    } else {
      console.error('获取医院列表失败:', response.data.msg);
    }
  } catch (error) {
    console.error('获取医院列表请求错误:', error);
  } finally {
    loading.value = false;
  }
};

// 选择医院
const selectHospital = (hospital: Hospital) => {
  // 跳转到开单申请页面
  router.push({ 
    path: '/drug-order', 
    query: { 
      hospitalId: hospital.id,
      hospitalName: hospital.hospitalName
    } 
  });
};

onMounted(() => {
  fetchHospitals();
});
</script>

<template>
  <div class="hospital-list-page">
    <van-nav-bar
      title="院区列表"
      left-arrow
      @click-left="goBack"
    >
      <template #right>
        <div class="nav-icons">
          <span class="dot"></span>
          <span class="dot"></span>
          <span class="dot"></span>
          <span class="circle-btn"></span>
        </div>
      </template>
    </van-nav-bar>

    <div class="content">
      <h3 class="section-title">请选择院区</h3>
      
      <van-loading v-if="loading" class="loading-spinner" />
      
      <div v-else class="hospital-list">
        <div 
          v-for="hospital in hospitals" 
          :key="hospital.id" 
          class="hospital-item"
          @click="selectHospital(hospital)"
        >
          <div class="hospital-icon">
            <img src="/images/hospital.png" alt="医院图标">
          </div>
          <div class="hospital-info">
            <div class="hospital-name">{{ hospital.hospitalName }}</div>
            <div class="hospital-address">{{ hospital.detailedAddress }}</div>
          </div>
          <span class="arrow">&gt;</span>
        </div>
        
        <!-- 示例数据，当API未返回数据时显示 -->
        <div v-if="hospitals.length === 0" class="hospital-list">
          <div class="hospital-item" @click="selectHospital({id: 1, hospitalName: '四川省保健院南苑'} as Hospital)">
            <div class="hospital-icon">
              <img src="/images/hospital.png" alt="医院图标">
            </div>
            <div class="hospital-info">
              <div class="hospital-name">四川省保健院南苑</div>
              <div class="hospital-address">四川省成都市锦江区圆通路666号</div>
            </div>
            <span class="arrow">&gt;</span>
          </div>
          
          <div class="hospital-item" @click="selectHospital({id: 2, hospitalName: '四川省保健院北苑'} as Hospital)">
            <div class="hospital-icon">
              <img src="/images/hospital.png" alt="医院图标">
            </div>
            <div class="hospital-info">
              <div class="hospital-name">四川省保健院北苑</div>
              <div class="hospital-address">四川省成都市锦江区圆通路666号</div>
            </div>
            <span class="arrow">&gt;</span>
          </div>
          
          <div class="hospital-item" @click="selectHospital({id: 3, hospitalName: '四川省保健院体检中心'} as Hospital)">
            <div class="hospital-icon">
              <img src="/images/hospital.png" alt="医院图标">
            </div>
            <div class="hospital-info">
              <div class="hospital-name">四川省保健院体检中心</div>
              <div class="hospital-address">四川省成都市锦江区圆通路666号</div>
            </div>
            <span class="arrow">&gt;</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.hospital-list-page {
  background: #f8f9fa;
  min-height: 100vh;
}

.nav-icons {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dot {
  width: 6px;
  height: 6px;
  background: #d8d8d8;
  border-radius: 50%;
  display: inline-block;
}

.circle-btn {
  width: 18px;
  height: 18px;
  border: 1.5px solid #d8d8d8;
  border-radius: 50%;
  display: inline-block;
  margin-left: 6px;
}

.content {
  padding: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 16px;
}

.loading-spinner {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.hospital-list {
  margin-top: 12px;
}

.hospital-item {
  background: #fff;
  border-radius: 10px;
  margin-bottom: 12px;
  padding: 16px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;
}

.hospital-icon {
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  overflow: hidden;
}

.hospital-icon img {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

.hospital-info {
  flex: 1;
}

.hospital-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.hospital-address {
  font-size: 13px;
  color: #999;
}

.arrow {
  color: #ccc;
  font-size: 18px;
  margin-left: 8px;
}
</style> 